package com.sitanyun.merchant.guide.frament.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseFragment;
import com.sitanyun.merchant.guide.bean.EnterBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.frament.presenter.impl.MyusersFramentFPresenterImpl;
import com.sitanyun.merchant.guide.frament.view.activity.EnterShopDailsActivity;
import com.sitanyun.merchant.guide.frament.view.inter.IMyusersFramentFView;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.view.activity.InvitetomakemoneyActivity;
import com.sitanyun.merchant.guide.weiht.DateUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceshop;
import com.zhy.http.okhttp.OkHttpUtils;

import androidx.fragment.app.Fragment;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 * 用户
 */
public class MyusersFrament extends BaseFragment implements IMyusersFramentFView {


    @BindView(R.id.tv_return)
    ImageView tvReturn;
    @BindView(R.id.tv_title_str)
    TextView tvTitleStr;
    @BindView(R.id.image_right_key)
    ImageView imageRightKey;
    @BindView(R.id.tv_right_key)
    TextView tvRightKey;
    @BindView(R.id.ff_main_layout)
    FrameLayout ffMainLayout;
    @BindView(R.id.user_shop)
    TextView userShop;
    @BindView(R.id.user_enter)
    TextView userEnter;
    @BindView(R.id.tv_shar)
    TextView tvShar;
    @BindView(R.id.tv_browse)
    TextView tvBrowse;
    @BindView(R.id.tv_browsehourse)
    TextView tvBrowsehourse;
    @BindView(R.id.Invite_friends)
    TextView InviteFriends;
    @BindView(R.id.promotion_money)
    TextView promotionMoney;
    @BindView(R.id.shar_dails)
    LinearLayout sharDails;
    int type=1;
    private MyusersFramentFPresenterImpl mIMyusersFramentFPresenter;
    private EnterBean enterBean;
    private EnterBean enterBean1;
    //    private static final String[] mTitle = {"门店总排名", "企业总排名"};
//    private static final BaseFragment[] size = new BaseFragment[]{new ShtopFragment(), new EnterFragment()};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_myusers_frament;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        vpView.setAdapter(new TabAdapter(getActivity(), getChildFragmentManager(), mTitle, size));
//        tabView.setViewPager(vpView);
        initstore();
    }

    @Override
    public void initView() {
        tvTitleStr.setText("个人中心");
        tvReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public <T> T request(int requestFlag) {
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {

    }

    @OnClick({R.id.user_shop, R.id.user_enter, R.id.Invite_friends, R.id.promotion_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //门店
            case R.id.user_shop:
                type=1;
                userShop.setTextColor(getResources().getColor(R.color.shop));
                userEnter.setTextColor(getResources().getColor(R.color.enter));
                initstore();
                break;
            //企业
            case R.id.user_enter:
                type=2;
                userEnter.setTextColor(getResources().getColor(R.color.shop));
                userShop.setTextColor(getResources().getColor(R.color.enter));
                initenter();
                break;
            //赚钱
            case R.id.Invite_friends:
                if (DateUtils.isFastClick()) {
                    return ;
                }
      startActivity(new Intent(getActivity(), InvitetomakemoneyActivity.class));

                break;
            //推广
            case R.id.promotion_money:
                break;
        }
    }

    //企业
    private void initenter() {
        OkHttpUtils.get().url(Urls.enterprise)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                 enterBean = new Gson().fromJson(response, EnterBean.class);
                if (enterBean.getCode()==0){
                    tvShar.setText(enterBean1.getData().getShareCnt() + "");
                    tvBrowse.setText(enterBean1.getData().getBrowseCnt() + "");
                    tvBrowsehourse.setText(enterBean1.getData().getBrowseCust() + "");
                }else {
                  //  ToastUtil.showToast(getActivity(),enterBean.getMsg()+"");
                }
            }
        });
    }

    //门店
    private void initstore() {
        OkHttpUtils.get().url(Urls.store)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))

                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                enterBean1 = new Gson().fromJson(response, EnterBean.class);
               if (enterBean1.getCode()==0){
                   tvShar.setText(enterBean1.getData().getShareCnt() + "");
                   tvBrowse.setText(enterBean1.getData().getBrowseCnt() + "");
                   tvBrowsehourse.setText(enterBean1.getData().getBrowseCust() + "");
               }else {
                //   ToastUtil.showToast(getActivity(),enterBean1.getMsg()+"");
               }

            }
        });
    }

    /**
     * 详情
     */

    @OnClick(R.id.shar_dails)
    public void onViewClicked() {
        SharedPreferenceshop.clearAll();
        //1 门店  2企业
          if (type==1){
              SharedPreferenceshop.SaveData("type","1");
              SharedPreferenceshop.SaveData("shopshars",enterBean1.getData().getShareCnt() + "");
              SharedPreferenceshop.SaveData("shopbrow",enterBean1.getData().getBrowseCnt() + "");
              SharedPreferenceshop.SaveData("shopbrowcust",enterBean1.getData().getBrowseCust() + "");
              startActivity(new Intent(getActivity(), EnterShopDailsActivity.class));
          }else if (type==2){
              SharedPreferenceshop.SaveData("type","2");
              SharedPreferenceshop.SaveData("shopshars",enterBean.getData().getShareCnt() + "");
              SharedPreferenceshop.SaveData("shopbrow",enterBean.getData().getBrowseCnt() + "");
              SharedPreferenceshop.SaveData("shopbrowcust",enterBean.getData().getBrowseCust() + "");
              startActivity(new Intent(getActivity(), EnterShopDailsActivity.class));
          }

    }
}
